<template>
  <div w-full h-screen>
    <div v-for="(item, index) in result" :key="index">
      <tpl_block v-if="item.type === 'whiteSpace'" :res="item" />
      <tpl_banner v-if="item.type === 'carousel'" :res="item" />
      <tpl_search v-if="item.type === 'search'" :res="item" />
      <tpl_menu v-if="item.type === 'menu'" :res="item" />
      <tpl_flex_one v-if="item.type === 'flexOne'" :res="item" />
      <tpl_left_one_right_two v-if="item.type === 'leftOneRightTwo'" :res="item" />
      <tpl_left_two_right_one v-if="item.type === 'leftTwoRightOne'" :res="item" />
      <tpl_flex_two v-if="item.type === 'flexTwo'" :res="item" />
      <tpl_flex_three v-if="item.type === 'flexThree'" :res="item" />
      <tpl_flex_five v-if="item.type === 'flexFive'" :res="item" />
      <tpl_flex_four v-if="item.type === 'flexFour'" :res="item" />
      <tpl_hot_zone v-if="item.type === 'flexHotArea'" :res="item" />
      <tpl_category_goods v-if="item.type === 'goodsCategoryFunc'" :res="item" />
      <tpl_card_banner v-if="item.type === 'carouselCard'" :res="item"></tpl_card_banner>
      <tpl_article_list v-if="item.type === 'articleList'" :res="item"></tpl_article_list>
    </div>
  </div>
  <!-- <div @click="toGuide">跳转导览</div>
  <div @click="toFuncPage">跳转功能页</div> -->
  <screen-saver :visible="showScreenSaver" @close="closeScreenSaver" />

  <!-- <h1>3546546</h1> -->
</template>

<script setup lang="ts">
import { getFloorData } from '@/api/index'
import tpl_banner from '@/pages/v3-template/tpl_banner.vue' //轮播图
import tpl_block from '@/pages/v3-template/tpl_block.vue' //空白
import tpl_flex_five from '@/pages/v3-template/tpl_flex_five.vue'
import tpl_flex_four from '@/pages/v3-template/tpl_flex_four.vue'
import tpl_flex_one from '@/pages/v3-template/tpl_flex_one.vue' //图片
import tpl_flex_three from '@/pages/v3-template/tpl_flex_three.vue'
import tpl_flex_two from '@/pages/v3-template/tpl_flex_two.vue'
import tpl_hot_zone from '@/pages/v3-template/tpl_hot_zone.vue'
import tpl_left_one_right_two from '@/pages/v3-template/tpl_left_one_right_two.vue'
import tpl_left_two_right_one from '@/pages/v3-template/tpl_left_two_right_one.vue'
import tpl_menu from '@/pages/v3-template/tpl_menu.vue' //菜单
import tpl_search from '@/pages/v3-template/tpl_search.vue' //搜索栏
import tpl_category_goods from '@/pages/v3-template/tpl_category_goods.vue'
import tpl_card_banner from '@/pages/v3-template/tpl_card_banner.vue'
import tpl_article_list from '@/pages/v3-template/tpl_article_list.vue'

import screenSaver from '@/components/screen-saver/index.vue'
import { useConfigStore } from '@/store/modules/config'
import { onLoad, onShow, onHide, onPageScroll } from '@dcloudio/uni-app'

const configStore = useConfigStore()
const standbyTime = configStore.config?.standbyTime
console.log('待机时间', standbyTime)
const result = ref<any>('')

const showScreenSaver = ref(false)
let screenSaverTimer: any = null
const isOpenScrrenSaver = ref(true)
const resetHomeTimer = () => {
  if (!isOpenScrrenSaver.value) {
    return
  }
  clearTimeout(screenSaverTimer)
  screenSaverTimer = setTimeout(() => {
    showScreenSaver.value = true
    init()
  }, standbyTime) // 60 秒无操作显示屏保
}

const closeScreenSaver = () => {
  showScreenSaver.value = false
  resetHomeTimer() // 关闭屏保后重置计时
  init()
}

async function init() {
  const res = await getFloorData()
  console.log('获取装修数据', res)
  let foolData = res.data.data.pageData ? JSON.parse(res.data.data.pageData) : ''
  if (foolData.version) {
    result.value = foolData.data
    console.log(result.value)
    // 说明是新版楼层装修的内容
  }
}

onMounted(() => {
  init()
  // 监听用户操作
})
onShow(() => {
  console.log('111')
  document.removeEventListener('touchstart', resetHomeTimer)
  document.addEventListener('touchstart', resetHomeTimer)
  isOpenScrrenSaver.value = true
  resetHomeTimer()
})

onHide(() => {
  isOpenScrrenSaver.value = false
  // 清理定时器和事件
  clearTimeout(screenSaverTimer)
  document.removeEventListener('touchstart', resetHomeTimer)
})

onPageScroll((e) => {
  resetHomeTimer()
})

defineExpose({
  init
})

onUnmounted(() => {
  clearTimeout(screenSaverTimer)
  document.removeEventListener('touchstart', resetHomeTimer)
})
</script>

<style scoped></style>
